package com.example.mqmanager.config;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;

@Component
public class DynamicRabbitMQConfig {
    
    // 存储不同Virtual Host的连接工厂
    private final Map<String, ConnectionFactory> connectionFactoryMap = new ConcurrentHashMap<>();
    
    private String host = "192.168.11.199";
    private int port = 5672;
    private String username = "test";
    private String password = "test";
    private String virtualHost = "/";

    public ConnectionFactory getConnectionFactory() {
        return getConnectionFactory(virtualHost);
    }
    
    public ConnectionFactory getConnectionFactory(String vhost) {
        return connectionFactoryMap.computeIfAbsent(vhost, k -> {
            CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
            connectionFactory.setHost(host);
            connectionFactory.setPort(port);
            connectionFactory.setUsername(username);
            connectionFactory.setPassword(password);
            connectionFactory.setVirtualHost(k);
            return connectionFactory;
        });
    }

    // Getters and Setters
    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getVirtualHost() {
        return virtualHost;
    }

    public void setVirtualHost(String virtualHost) {
        this.virtualHost = virtualHost;
    }
}